﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;

namespace SerialPortLib
{
    public class SerialPortLib
    {
        #region 变量定义

        private SerialPort _comPort;
        private string ReceivedStr;
        public PortParam _PortParam;
        #endregion


        #region 实例化
        public SerialPortLib()
        {
            _comPort = new SerialPort();
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(PortParam portParam)
        {
            _PortParam = portParam;
            _comPort = new SerialPort
            {
                PortName = _PortParam.Portname,
                BaudRate = _PortParam.Baudrate,
                Parity = _PortParam.Parity,
                StopBits = _PortParam.StopBits,
                DataBits = _PortParam.DataBits
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(string portname)
        {
            _comPort = new SerialPort
            {
                PortName = portname,
                BaudRate = 9600,
                Parity = Parity.None,
                StopBits = StopBits.One,
                DataBits = 8
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(string portname, int baudrate)
        {
            _comPort = new SerialPort
            {
                PortName = portname,
                BaudRate = baudrate,
                Parity = Parity.None,
                StopBits = StopBits.One,
                DataBits = 8
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(string portname, int baudrate, Parity parity)
        {
            _comPort = new SerialPort
            {
                PortName = portname,
                BaudRate = baudrate,
                Parity = parity,
                StopBits = StopBits.One,
                DataBits = 8
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(string portname, int baudrate, Parity parity,StopBits stopBits)
        {
            _comPort = new SerialPort
            {
                PortName = portname,
                BaudRate = baudrate,
                Parity = parity,
                StopBits = stopBits,
                DataBits = 8
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(string portname, int baudrate, Parity parity, StopBits stopBits, int databits)
        {
            _comPort = new SerialPort
            {
                PortName = portname,
                BaudRate = baudrate,
                Parity = parity,
                StopBits = stopBits,
                DataBits = databits
            };
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }

        public SerialPortLib(SerialPort serialPort)
        {
            _comPort = serialPort;
            _comPort.DataReceived += new SerialDataReceivedEventHandler(this.OnDataReceived);
        }
        #endregion

        #region 方法

        public void UpdatePortParam()
        {
            bool bIsOpen = false;
            if (_comPort.IsOpen)
            {
                bIsOpen = true;
                _comPort.Close();
            }
            _comPort.PortName = _PortParam.Portname;
            _comPort.BaudRate = _PortParam.Baudrate;
            _comPort.Parity = _PortParam.Parity;
            _comPort.StopBits = _PortParam.StopBits;
            if (bIsOpen)
            {
                _comPort.Open();
            }
        }

        public void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {

        }


        #endregion
    }

    public struct PortParam
    {
        public string Portname;
        public int Baudrate;
        public Parity Parity;
        public StopBits StopBits;
        public int DataBits;
    }
}
